﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HQWeb.SysManage.RoleManage
{
    public partial class RoleList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                BianData();
            }
        }
        private void BianData()
        {
            BLL.SysManage.T_PUB_ROLE roleBll = new BLL.SysManage.T_PUB_ROLE();
            List<Model.SysManage.T_PUB_ROLE> modelList = roleBll.GetModelList("");
            GridView1.DataSource = modelList;
            GridView1.DataBind();
        }

        #region 行编辑
        /// <summary>
        /// 点页面GridView1中的增中人员方法实现，页面转到RoleUser.aspx页，并传入角色的ID
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int row = (e.NewEditIndex);
            string id = GridView1.Rows[row].Cells[0].Text;
            Response.Redirect("RoleUser.aspx?id=" + id);
        }
        #endregion

        #region 行删除
        /// <summary>
        /// 点页面GridView1中的删除方法实现，页面刷新，并传入参数:角色的ID
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int row = Convert.ToInt32(e.RowIndex);
                int id = Convert.ToInt32(GridView1.Rows[row].Cells[0].Text);
                BLL.SysManage.T_PUB_ROLE roleBll = new BLL.SysManage.T_PUB_ROLE();
                string  strName = GridView1.Rows[row].Cells[1].Text;
                roleBll.DelteRole(Convert.ToInt32(id));
                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "删除" + strName + "角色", DateTime.Now);
                HttpContext.Current.Response.Write("<script language=javascript>window.parent.location.reload();</script>");
            }
            catch (Exception ex)
            {
                Universal.JS.Alert(ex.Message);
                return;
            }
        }
        #endregion

        #region 行绑定数据触发后台事件
        /// <summary>
        /// 页面GridView中的DataKeyNames绑定到数据源中的一条记录(绑定ID)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Visible = false; //隐藏列,可以取得该隐藏列的信息 
            }
        }
        #endregion

        #region GridView1分页方法
        /// <summary>
        /// GridView1分页方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //GridView1.PageIndex = e.NewPageIndex;
            try
            {
                int newPageIndex = 0;

                if (-2 == e.NewPageIndex)
                {
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = GridView1.Controls[0].Controls[GridView1.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                    if (null != pagerRow)
                    {
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                    }

                    if (null != txtNewPageIndex)
                    {
                        newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                    }

                    newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                    newPageIndex = newPageIndex >= GridView1.PageCount ? GridView1.PageCount - 1 : newPageIndex;
                    GridView1.PageIndex = newPageIndex;
                }
                else if (e.NewPageIndex == -1)
                {
                    newPageIndex = 0;
                    GridView1.PageIndex = 0;
                }
                else
                {
                    newPageIndex = e.NewPageIndex;
                    GridView1.PageIndex = e.NewPageIndex;
                }
                BianData();
            }
            catch (Exception ex)
            {
               // COM.HS.Utility.JScript.Alert(ex.Message);
              //  return;
            }
        }
        #endregion

    }
}